package com.example.yuekaoceshi.http;



import android.database.DatabaseUtils;
import android.widget.Toast;

import com.example.yuekaoceshi.MyApp;
import com.example.yuekaoceshi.bean.Bean;
import com.example.yuekaoceshi.bean.Info;
import com.hua.mydemo.dao.DaoMaster;
import com.hua.mydemo.dao.DaoSession;
import com.hua.mydemo.dao.InfoDao;

import java.util.List;

/**
 * Created by 段傅华 on 2018/12/11.
 */

public class DataBaseUtils {
   private static DataBaseUtils mDataBaseUtil;
    final InfoDao mInfoDao;

    private DataBaseUtils(){
       //初始化数据库
       DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(MyApp.app, "hua.db");
       //获取可读写数据库
       DaoMaster daoMaster = new DaoMaster(helper.getWritableDatabase());
       //获取表管理器
       DaoSession daoSession = daoMaster.newSession();
       //获取我们要操作表的工具类
        mInfoDao = daoSession.getInfoDao();
   }
   public static DataBaseUtils getInstance(){
       if (mDataBaseUtil==null){
           synchronized (DatabaseUtils.class){
               if (mDataBaseUtil==null){
                   mDataBaseUtil=new DataBaseUtils();
               }
           }
       }
       return mDataBaseUtil;
   }
  //插入数据
    public void insert(List<Info> beans){
       mInfoDao.insertInTx(beans);
    }
    //查询
    public List<Info> selectAll(){
        return mInfoDao.queryBuilder().list();
    }
    //删除
    public void delect(Info info){
        mInfoDao.deleteInTx(info);
    }
}
